package com.xxgc.common.quartz.utils;

import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.stereotype.Component;

/**
 * @program: SpringCloudStudy
 * @description:
 * @author: xiaoliu
 * @create: 2022-12-05 21:07
 **/
@Component
@Slf4j
public class QuartzUtil {
    //使用默认的配置
    private static final SchedulerFactory schedulerFactory = new StdSchedulerFactory();
    /**
     * 新增任务
     * @param jobName  任务名称
     * @param cron  cron表达式
     * @param jobClass 计时器任务实体类
     */
    public void addJob(String jobName,
                              String jobGroup,
                              String triggerName,
                              String triggerGroup,
                              String cron,
                              Class<? extends Job> jobClass) throws SchedulerException {
        //创建任务
        JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName,jobGroup).build();
        //创建触发器 每 5 秒钟执行一次
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity(triggerName,triggerGroup)
                .withSchedule(CronScheduleBuilder.cronSchedule(cron))
                .build();

        //获取实例化的 Scheduler。
        Scheduler scheduler = getScheduler();
        //将任务及其触发器放入调度器
        scheduler.scheduleJob(jobDetail, trigger);
        //调度器开始调度任务
        if (!scheduler.isShutdown()) {
            scheduler.start();
            log.info(String.format("启动任务，jobName：%s", jobName));
        }
    }


    /**
     * 删除任务
     * @param jobName
     */
    public void deleteJob(
            String jobName,
            String jobGroup,
            String triggerName,
            String triggerGroup) throws SchedulerException {
        TriggerKey triggerKey = TriggerKey.triggerKey(triggerName,triggerGroup);
        Scheduler scheduler = getScheduler();
        scheduler.pauseTrigger(triggerKey);   // 停止触发器
        scheduler.unscheduleJob(triggerKey);  // 移除触发器
        scheduler.deleteJob(JobKey.jobKey(jobName,jobGroup));
        log.info("删除任务，jobName："+jobName+"jobGroup:"+jobGroup);
    }

    /**
     * 暂停任务
     * @param triggerName
     * @param triggerGroup
     * @throws SchedulerException
     */
    public void suspendJob(
            String triggerName,
            String triggerGroup) throws SchedulerException {
        TriggerKey triggerKey = TriggerKey.triggerKey(triggerName,triggerGroup);
        Scheduler scheduler = getScheduler();

        scheduler.pauseTrigger(triggerKey);   // 停止触发器
        log.info("暂停任务，triggerGroup："+triggerGroup+"triggerName:"+triggerName);
    }

    /**
     * 运行任务
     * @param triggerName
     * @param triggerGroup
     * @throws SchedulerException
     */
    public void runJob(
            String triggerName,
            String triggerGroup) throws SchedulerException {
        TriggerKey triggerKey = TriggerKey.triggerKey(triggerName,triggerGroup);
        Scheduler scheduler = getScheduler();
        scheduler.resumeTrigger(triggerKey);   // 运行触发器
        log.info("继续任务，triggerGroup："+triggerGroup+"triggerName:"+triggerName);
    }


    //获取一个默认的Scheduler对象
    private Scheduler getScheduler() {
        try {
            return schedulerFactory.getScheduler();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return null;
    }

    //停止任务--再启动需要从新初始化
    public void stopScheduler() {
        Scheduler scheduler = getScheduler();
        try {
            /**
             *  shutdown(true)表示等待所有正在执行的任务执行完毕后关闭Scheduler
             *  shutdown(false),即shutdown()表示直接关闭Scheduler
             */
            scheduler.shutdown();
            log.info("------停止任务----");
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    //暂停所有任务执行
    public void standbyScheduler() {
        Scheduler scheduler = getScheduler();
        try {
            scheduler.standby();
            log.info("-----暂停任务----");
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

}
